package com.tp;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Intent;
import android.net.ParseException;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SearchTrip extends CustomWindow implements OnClickListener {
	EditText name;
	Button search_trip;
	static JSONArray jArr;
	static ArrayList<String> trip = new ArrayList<String>();
	static int day;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search_trip);
		this.title.setText("Tìm ngày hành trình");
		this.icon.setImageResource(R.drawable.search);
		name = (EditText) findViewById(R.id.search_trip_name);
		search_trip = (Button) findViewById(R.id.search_trip);
		search_trip.setOnClickListener(this);

	}

	private void getTrip(String name) {
		// TODO Auto-generated method stub
		JSONArray jArray;
		JSONObject json_data = null;
		jArr = new JSONArray();
		trip.clear();
		int i;
		int id_Obj, type;
		ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
		postParameters.add(new BasicNameValuePair("name", name));

		String result = null;
		try {
			result = CustomHttpClient.executeHttpPost(Constant.host
					+ "/ObjectController/getObjectByName", postParameters);

			if (result != "") {

				jArray = new JSONArray(result);

				for (i = 0; i < jArray.length(); i++) {
					json_data = jArray.getJSONObject(i);
					id_Obj = json_data.getInt("id_Obj");
					type = json_data.getInt("id_Type");
					if (type == 1) {
						getTripDetail(id_Obj);
						// trip_name = getTripById(id_Trip);

					} else if (type == 2 || type == 3) {
						getTripDetailResEvent(id_Obj, type);

					}
				}
			}
		} catch (JSONException e1) {

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void getTripDetailResEvent(int id_Obj, int type) {
		// TODO Auto-generated method stub
		JSONArray jArray;
		JSONObject json_data = null;

		int i;
		int id_TripDetail;
		ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
		postParameters.add(new BasicNameValuePair("id_Obj", Integer
				.toString(id_Obj)));
		postParameters.add(new BasicNameValuePair("type", Integer
				.toString(type)));

		String result = null;
		try {
			result = CustomHttpClient
					.executeHttpPost(Constant.host
							+ "/TripController/getTripDetailByResEvent",
							postParameters);

			if (result != "") {

				jArray = new JSONArray(result);

				for (i = 0; i < jArray.length(); i++) {
					json_data = jArray.getJSONObject(i);
					// JSONObject data = new JSONObject();
					id_TripDetail = json_data.getInt("id_TripDetail");
					// Trip.day_ = json_data.getInt("day");
					getTripByIdTripDetail(id_TripDetail);

				}
			}
		} catch (JSONException e1) {

		} catch (ParseException e1) {

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void getTripByIdTripDetail(int id_TripDetail) {
		JSONArray jArray;
		JSONObject json_data = null;

		int i;
		int id_Trip;
		ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
		postParameters.add(new BasicNameValuePair("id_TripDetail", Integer
				.toString(id_TripDetail)));

		String result = null;
		try {
			result = CustomHttpClient.executeHttpPost(Constant.host
					+ "/TripController/getTripByIdTripDetail", postParameters);

	
		if (result != "") {
			
				jArray = new JSONArray(result);

				for (i = 0; i < jArray.length(); i++) {

					json_data = jArray.getJSONObject(i);
					// JSONObject data = new JSONObject();
					id_Trip = json_data.getInt("id_Trip");
					// Trip.day_ = json_data.getInt("day");
					JSONObject trip_data = getTripById(id_Trip);
					trip_data.put("day", json_data.getInt("day"));
					/*
					 * data.put("id_Trip", id_Trip); data.put("name",
					 * trip_data.getString("name"));
					 */

					int j;
					for (j = 0; j < trip.size(); j++) {
						if (trip.get(j).equals(
								trip_data.getString("name") + ", Ngày "
										+ trip_data.getString("dateStart")
										+ ", Ngày thứ "
										+ json_data.getInt("day")))
							break;
					}
					if (j == trip.size()) {
						jArr.put(trip_data);
						trip.add(trip_data.getString("name") + ", Ngày "
								+ trip_data.getString("dateStart")
								+ ", Ngày thứ " + json_data.getInt("day"));
					}

				}
			}
		}catch (JSONException e1) {

			} catch (ParseException e1) {

		} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}


	private void getTripDetail(int id_Obj) {
		// TODO Auto-generated method stub
		JSONArray jArray;
		JSONObject json_data = null;

		int i;
		int id_Trip;
		ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
		postParameters.add(new BasicNameValuePair("id_Hotel", Integer
				.toString(id_Obj)));

		String result = null;
		try {
			result = CustomHttpClient.executeHttpPost(Constant.host
					+ "/TripController/getTripByHotel", postParameters);

			if (result != "") {

				jArray = new JSONArray(result);

				for (i = 0; i < jArray.length(); i++) {
					json_data = jArray.getJSONObject(i);
					// JSONObject data = new JSONObject();
					id_Trip = json_data.getInt("id_Trip");
					// Trip.day_ = json_data.getInt("day");
					JSONObject trip_data = getTripById(id_Trip);
					trip_data.put("day", json_data.getInt("day"));
					/*
					 * data.put("id_Trip", id_Trip); data.put("name",
					 * trip_data.getString("name"));
					 */
					int j;
					for (j = 0; j < trip.size(); j++) {
						if (trip.get(j).equals(
								trip_data.getString("name") + ", Ngày "
										+ trip_data.getString("dateStart")
										+ ", Ngày thứ "
										+ json_data.getInt("day")))
							break;
					}
					if (j == trip.size()) {
						jArr.put(trip_data);
						trip.add(trip_data.getString("name") + ", Ngày "
								+ trip_data.getString("dateStart")
								+ ", Ngày thứ " + json_data.getInt("day"));
					}

				}
			}
		} catch (JSONException e1) {

		} catch (ParseException e) {

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private JSONObject getTripById(int id_Trip) {
		// TODO Auto-generated method stub
		JSONArray jArray;
		JSONObject json_data = null;

		int i;

		ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
		postParameters.add(new BasicNameValuePair("id_Trip", Integer
				.toString(id_Trip)));

		String result = null;
		try {
			result = CustomHttpClient.executeHttpPost(Constant.host
					+ "/TripController/getTripById", postParameters);

			if (result != "") {

				jArray = new JSONArray(result);

				for (i = 0; i < jArray.length(); i++) {
					json_data = jArray.getJSONObject(i);
					/*
					 * trip_name = json_data.getString("name");
					 * 
					 * trip_name+=", Ngày " + json_data.getString("startDate");
					 */
				}
			}
		} catch (JSONException e1) {

		} catch (ParseException e) {

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return json_data;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.search_trip:
			getTrip(name.getText().toString());
			if (trip.size() > 0) {
				ListTrip.flag = 1;
				startActivity(new Intent(this, ListTrip.class));
			} else
				Toast.makeText(this, "Không tìm thấy", Toast.LENGTH_SHORT)
						.show();
			break;
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_search_trip, menu);
		return true;
	}

}
